<template>
  <view>
    <!-- 搜索 -->
    <my-search></my-search>
    <!-- 轮播 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" class="my-swiper">
      <swiper-item v-for="(v, i) in banners" :key="i">
        <navigator class="swiper-item" :url="`/subpkg/detail/detail?goods_id=${v.goods_id}`">
          <image :src="v.image_src" class="banner"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航 -->
    <view class="menus">
      <view class="menu-item" v-for="(v, i) in menus" :key="i" @click="handleClick(v)">
        <image :src="v.image_src" mode="heightFix"></image>
      </view>
    </view>
    <!-- 列表导航 -->
    <view class="list">
      <view class="list-item" v-for="(v, i) in list" :key="i">
        <view class="top">
          <image :src="v.floor_title.image_src" mode="widthFix"></image>
        </view>
        <view class="bottom">
          <navigator class="left" :url="v.product_list[0].url">
            <image :src="v.product_list[0].image_src" mode="widthFix"
              :style="{ width: v.product_list[0].image_width + 'rpx' }">
            </image>
          </navigator>
          <view class="right">
            <navigator :url="v.url" class="right-box" v-for="(v, i) in v.product_list" :key="i" v-if="i !== 0">
              <image :src="v.image_src" mode="widthFix" :style="{ width: v.image_width + 'rpx' }"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import mixin from '../../mixins/index.js'
  export default {
    mixins: [mixin],
    data() {
      return {
        banners: [],
        menus: [],
        list: []
      };
    },
    methods: {
      handleClick(v) {
        if (v.name === '分类') {
          // 进入分类页面
          uni.switchTab({
            url: '/pages/classify/classify'
          })
        }
      },
      async getList() {
        const [error, resp] = await uni.request({
          url: 'https://www.uinav.com/api/public/v1/home/floordata'
        });
        if (error) {
          console.log('请求列表数据报错', error);
          return;
        }
        // 数据二次处理
        resp.data.message.forEach(v => {
          v.product_list.forEach(v => {
            v.url =
              `/subpkg/detail_list/detail_list?name=${v.name}&query=${v.navigator_url.slice(v.navigator_url.indexOf('=') + 1)}`
          })
        })
        // 保存
        this.list = resp.data.message
      },
      async getMenus() {
        const [error, resp] = await uni.request({
          url: 'https://www.uinav.com/api/public/v1/home/catitems'
        });
        if (error) {
          console.log('请求导航数据报错', error);
          return;
        }
        this.menus = resp.data.message
      },
      async getBanners() {
        const [error, resp] = await uni.request({
          url: 'https://www.uinav.com/api/public/v1/home/swiperdata'
        });
        if (error) {
          console.log('请求轮播数据报错', error);
          return;
        }
        this.banners = resp.data.message
      },
    },
    onLoad() {
      this.getBanners()
      this.getMenus()
      this.getList()
    }
  }
</script>

<style lang="scss">
  .my-swiper {
    width: 100%;
    height: 400upx;

    .banner {
      width: 100%;
      height: 400upx;
    }
  }

  .menus {
    display: flex;
    justify-content: space-around;

    .menu-item {

      image {
        height: 120upx;
      }
    }
  }


  .list {
    padding: 10upx;

    .list-item {
      .top {
        image {
          width: 100%;
          height: 60upx;
        }
      }

      .bottom {
        display: flex;

        .right {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
        }
      }
    }
  }
</style>
